<template>
  <view class="feedback-container">
    <view class="content-area">
      <uni-card class="view-title" :title="name">
        <up-row customStyle="margin-bottom: 10px">
            <up-col span="12">
                <h1>STATION NO. {{ no }}</h1>
            </up-col>
        </up-row>
        <up-row customStyle="margin-bottom: 10px">
            <up-col span="12">
                <up-textarea v-model="content" maxlength="300" placeholder="請輸入內容" autoHeight count focus height="200" @confirm="submitContent" confirmType="done"></up-textarea>
            </up-col>
        </up-row>
      </uni-card>
    </view>

    <!-- 操作按钮区域 -->
    <view class="button-container">
      <up-button
        type="default"
        size="large"
        plain
        @click="goBack"
        class="back-button"
      >
        返回
      </up-button>
      <up-button
        type="primary"
        size="large"
        @click="submitContent"
        :disabled="!content.trim()"
        class="submit-button"
      >
        提交反馈
      </up-button>
    </view>

    <up-modal :show="showModal" title="提交確認" content='是否確認提交該內容，一旦確認無法修改' @cancel="closeModal" @confirm="confirmModal" showCancelButton="true"></up-modal>
  </view>
</template>

<script>
import { submitAMessage } from '@/api/system/map'
import { useUserStore } from '@/stores';
  export default {
    data() {
      return {
        showModal:false,
        title: 'Leave feedback',
        no:'',
        name:'',
        id:'',
        content: '',
        memberId: useUserStore().user_id
      }
    },
    onLoad(options) {
      this.id = options.id
      this.no = options.no
      this.name = options.name
      uni.setNavigationBarColor({
          frontColor: '#000',
          backgroundColor: 'rgb(112, 153, 180)',
          animation: {
              duration: 400,
              timingFunc: 'easeIn'
          }
      })
    },
  methods: {
    closeModal(){
      this.showModal = false;
    },
    confirmModal(){
      this.closeModal();
      const parameter = {
        content: this.content,
        productId: this.id,
        productNo: this.no,
        productName: this.name,
        createBy: this.memberId
      }
      submitAMessage(parameter).then((data) => {
        uni.navigateBack({
          delta: 1
        });
        uni.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
      }).catch(error => {
          console.error("获取数据失败:", error);
      })
    },
    submitContent(){
      if (!this.content.trim()) {
        uni.showToast({
          title: '請輸入反饋內容',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      this.showModal = true;
    },
    goBack(){
      uni.navigateBack({
        delta: 1
      });
    }
  }
  }
</script>

<style scoped>
  page {
    background-color: #ffffff;
  }

  .feedback-container {
    height: 100vh;
    display: flex;
    flex-direction: column;
  }

  .content-area {
    flex: 1;
    padding-bottom: 140rpx;
    overflow-y: auto;
  }

  .view-title {
    font-weight: bold;
  }

  .view-content {
    font-size: 26rpx;
    padding: 12px 5px 0;
    color: #333;
    line-height: 24px;
    font-weight: normal;
  }

  .button-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 20rpx 30rpx;
    padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
    background-color: #ffffff;
    border-top: 1px solid #f0f0f0;
    display: flex;
    gap: 20rpx;
    z-index: 100;
  }

  .back-button {
    flex: 1;
    border-color: #ccc;
    color: #666;
  }

  .submit-button {
    flex: 2;
    background-color: rgb(112, 153, 180);
    border-color: rgb(112, 153, 180);
  }

  .submit-button[disabled] {
    background-color: #f0f0f0;
    border-color: #f0f0f0;
    color: #ccc;
  }
</style>
